<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('page_setting');
	$params=array('mid'=>$_SESSION['uid']);
	$return['commission_money']=$this->sum('commission',$params,'money');
	if(!$return['commission_money']){
		$return['commission_money']=0;
	}
	$return['commission_total']=$this->count('commission',$params);
	$params['type']=20;
	unset($params['level']);
	$mids=array($_SESSION['uid']);
	$return['list']=array();
	$return['params']=array(
		'type'=>intval($_RQ['type'])?:3,
	);
	$return['psize']=20;
	set_html('title',$return['setting']['torder']?:'代理订单');
	$return['xuniname']=$this->xuniname;
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$type=intval($_RQ['type']);
	if(!$type){
		$type=3;
	}
	if(!$return['pluginset']['isorderall']){
		$params['type']=$type;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('commission',$parameter);
	if(!$list){
		exi('none');
	}
	foreach($list as &$item){
		$item['member']=model('core/member')->basic($item['mid']);
		$item['statustitle']=$this->commission_status[$item['status']];	
		$item['typetitle']=$this->commission_money_types[$item['type']];
		$item['createtime']=date('Y-m-d',$item['createtime']);
	}
	exi($list);
}
if($_RQ['op']=='daylist'){
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'))+1;	
	$pindex=intval($_RQ['page'])?:1;
	$pagetime=$daytime - ($pindex-1)*20*24*3600;
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$list=array();
	for($i=0;$i<20;$i++){
		$endtime=$pagetime-$i*24*3600;
		$starttime=$endtime-24*3600;
		$params['createtime between']=array($starttime,$endtime);
		$money=$this->getsum('commission',$params,'money');
		$list[]=array(
			'day'=>date('Y-m-d',$starttime),
			'money'=>$money,
		);
	}
	if(!$list){
		exi('none');
	}
	exi($list);
}
if($_RQ['op']=='getitem'){
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$params['day']=$_RQ['day'];
	$commission_money_types=$this->commission_money_types;
	$list=array();
	foreach($commission_money_types as $k=>$title){
		$params['type']=$k;
		$jifen=0;
		$money=0;
		$isjifen=0;
		if($k==1||$k==2||$k==8){
			$jifen=pdo_getsum('jy_baojdai_commission',$params,'jifen');
			$isjifen=1;
		}else{
			$money=pdo_getsum('jy_baojdai_commission',$params,'money');
		}
		$list[]=array(
			'type'=>$k,
			'day'=>$_RQ['day'],
			'money'=>$money,
			'jifen'=>$jifen,
			'isjifen'=>$isjifen,
			'typetitle'=>$title
		);
	}
	paixu($list,'isjifen','desc');
	exi($list);
}
if($_RQ['op']=='xunibalance'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'isxuni'=>1);
	$money=pdo_getsum('jy_baojdai_commission',$params,'money');
	cfc('core')->member_credit($_SESSION['uid'],'credit'.$this->setting['credit_type'],$money,true,'jy_baojdai','代理奖励结算');
	$update=array(
		'status'=>2,
	);
	$this->update('commission',$update,$params);
	exi('结算成功！','','/index/');
}
?>